<template>
  <div class="app-container">
    <!--    顶部搜索栏-->
    <el-form
      :inline="true"
      :model="listParams"
      class="demo-form-inline"
      style="margin-left: 30px"
    >
      <el-row>
        <el-col :span="20">
          <el-form-item label="昵称">
            <el-input
              v-model="listParams.nickname"
              placeholder="昵称"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="账号">
            <el-input
              v-model="listParams.accountId"
              placeholder="账号"
              clearable
              style="width: 150px"
            />
          </el-form-item>
          <el-form-item label="角色">
            <el-select
              v-model="listParams.role"
              clearable
              placeholder="角色"
              style="width: 130px"
            >
              <el-option label="普通用户" value="0" />
              <el-option label="管理员" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="fetchData(listParams)">
              <i class="el-icon-zoom-in" />
              <span>查询</span>
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="账号" width="200">
        <template slot-scope="scope">
          <div class="accountWrapper">
            <el-avatar size="number" src="">
              <img :src="scope.row.avatar" />
            </el-avatar>
            <span>{{ scope.row.accountId }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="昵称">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="手机号" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column
        class-name="status-col"
        label="权限"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag :type="scope.row.role | statusFilter">{{
            scope.row.role === 1 ? "管理员" : "普通用户"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="操作"
        width="300"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="warning" @click="changeRoleStart(scope.row)">
            更改权限
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    添加的弹窗-->
    <el-dialog title="创建账号" :visible.sync="dialogAddFormVisible" center>
      <el-form :model="accountInfoForm">
        <el-form-item label="昵称">
          <el-input v-model="accountInfoForm.nickname" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="accountInfoForm.phone" />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="accountInfoForm.password" />
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input v-model="accountInfoForm.confirmPassword" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addAccount">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="更改权限"
      :visible.sync="dialogRoleFormVisible"
      center
      width="400px"
    >
      <el-form :model="roleForm">
        <el-form-item label="选择权限">
          <el-select
            v-model="roleForm.role"
            placeholder="角色"
            style="width: 130px"
          >
            <el-option label="普通用户" value="0" />
            <el-option label="管理员" value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogRoleFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeRole">确 定</el-button>
      </div>
    </el-dialog>
    <!--    底部分页    -->
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="listParams.pageNum"
        :page-size="listParams.pageSize"
        style="
          display: flex;
          align-items: center;
          justify-content: center;
          margin-top: 10px;
        "
        @current-change="changePage($event)"
      />
    </el-footer>
  </div>
</template>
>
<script>
import * as accountApi from "@/api/account";
import { Message } from "element-ui";

export default {
  name: "AccountList",
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: "success",
        0: "gray",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: "",
      dialogAddFormVisible: false,
      dialogRoleFormVisible: false,
      listParams: {
        pageSize: 10,
        pageNum: 1,
        accountId: "",
        nickname: "",
        phone: "",
        role: "",
      },
      accountInfoForm: {
        nickname: "",
        phone: "",
        password: "",
        confirmPassword: "",
      },
      roleForm: {
        accountId: "",
        role: "",
      },
    };
  },
  created() {
    this.fetchData(this.listParams);
  },
  methods: {
    fetchData(listParams) {
      this.listLoading = true;
      accountApi.getAccountList(listParams).then((response) => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    changePage(event) {
      this.listParams.pageNum = event;
      this.fetchData(this.listParams);
    },
    changeRoleStart(accountInfo) {
      this.dialogRoleFormVisible = true;
      this.roleForm.accountId = accountInfo.accountId;
      this.roleForm.role = String(accountInfo.role);
    },
    changeRole() {
      console.log(this.roleForm);
      accountApi.updateAccount({ ...this.roleForm }).then((res) => {
        Message.success(res.message);
        this.fetchData(this.listParams);
        this.dialogRoleFormVisible = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.accountWrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  span {
    margin-left: 10px;
  }
}
</style>
